package dice.sinanya.dice.manager;

import com.forte.qqrobot.beans.messages.types.MsgGetTypes;
import dice.sinanya.entity.EntityTypeMessages;
import dice.sinanya.exceptions.CantInPrivateException;
import dice.sinanya.exceptions.NotSetKpGroupException;

import static dice.sinanya.system.MessagesKp.KP_GROUP;
import static dice.sinanya.system.MessagesKp.KP_ID;
import static dice.sinanya.tools.checkdata.CheckCantInPrivate.checkCantInPrivate;
import static dice.sinanya.tools.getinfo.GetMessagesProperties.entitySystemProperties;
import static dice.sinanya.tools.getinfo.Kp.*;
import static dice.sinanya.tools.makedata.Sender.sender;

/**
 * @author SitaNya
 * 日期: 2019-06-15
 * 电子邮箱: sitanya@qq.com
 * 维护群(QQ): 162279609
 * 有任何问题欢迎咨询
 * 类说明: kp主群
 */
public class Kp {
    private final EntityTypeMessages entityTypeMessages;

    public Kp(EntityTypeMessages entityTypeMessages) {
        this.entityTypeMessages = entityTypeMessages;
    }

    /**
     * 设定kp主群，由于基本不造成影响，所以不做删除了
     */
    public void set() throws CantInPrivateException {
        checkCantInPrivate(entityTypeMessages);
        setKpGroup(entityTypeMessages, entityTypeMessages.getFromGroupString());
        String tmpGroup = KP_GROUP.getOrDefault(entityTypeMessages.getFromQqString(), "0");
        KP_GROUP.put(entityTypeMessages.getFromQqString(), entityTypeMessages.getFromGroupString());
        KP_ID.remove(tmpGroup);
        KP_ID.put(entityTypeMessages.getFromGroupString(), entityTypeMessages.getFromQqString());
        sender(entityTypeMessages, entitySystemProperties.getSetKp(entityTypeMessages.getFromGroupString()));
    }

    /**
     * 设定kp主群，由于基本不造成影响，所以不做删除了
     */
    public void exit() throws CantInPrivateException {
        checkCantInPrivate(entityTypeMessages);
        if (entityTypeMessages.getMsgGetTypes().equals(MsgGetTypes.privateMsg)){
            delKpQq(entityTypeMessages);
            String tmpGroup = KP_GROUP.getOrDefault(entityTypeMessages.getFromQqString(), "0");
            KP_GROUP.remove(entityTypeMessages.getFromQqString());
            KP_ID.remove(tmpGroup);
            sender(entityTypeMessages, String.format("已移除你的kp设定:" + entityTypeMessages.getFromGroupString()));
        }else {
            delKpGroup(entityTypeMessages.getFromGroupString());
            String tmpId = KP_ID.getOrDefault(entityTypeMessages.getFromGroupString(), "0");
            KP_GROUP.remove(tmpId);
            KP_ID.remove(entityTypeMessages.getFromGroupString());
            sender(entityTypeMessages, String.format("已移除本群的kp设定:" + entityTypeMessages.getFromGroupString()));
        }
    }

    /**
     * 获取此kp当前设定的主群群号
     *
     * @return 获取kp主群群号
     * @throws NotSetKpGroupException 没找到则报错并原渠道回复报错语，具体语句配置文件中可以修改
     */
    public String get() throws NotSetKpGroupException {
        return getKpGroup(entityTypeMessages);
    }

}
